<template :onkeydown="down">
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @click="dataFormSubmit()" label-width="80px">
    <el-form-item label="帖子内容" prop="text">
     <!--  <el-input v-model="dataForm.text" placeholder="内容"></el-input> -->
   <!--      <single-upload v-model="images"  :data="dataForm.text"></single-upload> -->
     <el-upload
      action="http://help-xiao.oss-cn-beijing.aliyuncs.com"
      :data="dataObj"
      list-type="picture"
      :multiple="false" :show-file-list="showFileList"
      :file-list="fileList"
      :before-upload="beforeUpload"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :on-preview="handlePreview">
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过10MB</div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%"  alt="">
    </el-dialog>
     <template >
           <quill-editor 
      ref="myQuillEditor"
      v-model="dataForm.text"
      :options="editorOption"
    />
     </template>
    </el-form-item>
<!--     <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item> -->
<!--     <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item> -->
    <el-form-item label="置顶" prop="isTop">
 <!--      <el-input v-model="dataForm.isTop" placeholder="置顶"></el-input> -->
 <el-switch
  v-model="dataForm.isTop"
            :active-value="1"
            :inactive-value="0"
  active-color="#13ce66"
  inactive-color="#ff4949">
</el-switch>
    </el-form-item>
    <el-form-item label="封面" prop="imge">
      <!-- <el-input v-model="dataForm.imge" placeholder="封面"></el-input> -->
         <single-upload v-model="dataForm.imge"></single-upload>
    </el-form-item>
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="副标题" prop="summary">
      <el-input v-model="dataForm.summary" placeholder="副标题"></el-input>
    </el-form-item>
    <el-form-item label="分类名" prop="categoryId">
      <!-- <el-input v-model="dataForm.categoryId" placeholder="分类id"></el-input> -->
  <el-select v-model="dataForm.categoryId" placeholder="请选择">
    <el-option
      v-for="item in catesList"
      :key="item.id"
      :label="item.tltle"
      :value="item.id">
    </el-option>
  </el-select>

    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
   import {policy} from '@/components/upload/policy'
   import { getUUID } from '@/utils'
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import singleUpload from "@/components/upload/singleUpload"
import { quillEditor } from "vue-quill-editor";
  export default {
      name: 'singleUploads',
    props: {
      value: String
    },
     components: {
    quillEditor,
    singleUpload
  },
  created(){
  },
    data () {
      return {
         catesList:[],
        visible: false,
        images:"",
        dataForm: {
          id: 0,
          text: '',
        /*   createTime: '', */
          isTop: '',
          imge: '',
          title: '',
          summary: '',
          categoryId: ''
        },
        dataRule: {
          text: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ],
       /*    createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ], */
  
          isTop: [
            { required: true, message: '置顶不能为空', trigger: 'blur' }
          ],
          imge: [
            { required: true, message: '封面不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          summary: [
            { required: true, message: '副标题不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: true, message: '分类id不能为空', trigger: 'blur' }
          ]
        },
        dataObj: {
          policy: '',
          signature: '',
          key: '',
          ossaccessKeyId: '',
          dir: '',
          host: '',
          // callback:'',
        },
        dialogVisible: false
      }
    }
    ,
  computed: {

    imageUrl() {
        return this.value;
      },
      imageName() {
        if (this.value != null && this.value !== '') {
          return this.value.substr(this.value.lastIndexOf("/") + 1);
        } else {
          return null;
        }
      },
      fileList() {
        return [{
          name: this.imageName,
          url: this.imageUrl
        }]
      },
      showFileList: {
        get: function () {
          return this.value !== null && this.value !== ''&& this.value!==undefined;
        },
        set: function (newValue) {
        }
      }
  },
    
    methods: {
             //获取分类的数据
           getCatesList(){
            this.$http({
              url: this.$http.adornUrl(`/mybog/category/list`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(res=>{
               this.catesList=res.data.page.list
               console.log(this.catesList)
            })

           },   

        emitInput(val) {
          this.dataForm.text= this.dataForm.text+"<br><img src="+val+"  >"
          console.log(val)
        this.$emit('input', val)
      },
      handleRemove(file, fileList) {
        this.emitInput('');
      },
      handlePreview(file) {
        this.dialogVisible = true;
      },
      beforeUpload(file) {
        let _self = this;
        return new Promise((resolve, reject) => {
          policy().then(response => {
            _self.dataObj.policy = response.data.policy;
            _self.dataObj.signature = response.data.signature;
            _self.dataObj.ossaccessKeyId = response.data.accessid;
            _self.dataObj.key = response.data.dir +getUUID()+'_${filename}';
            _self.dataObj.dir = response.data.dir;
            _self.dataObj.host = response.data.host;
            resolve(true)
          }).catch(err => {
            reject(false)
          })
        })
      },
      handleUploadSuccess(res, file) {
        console.log("上传成功...")
        this.showFileList = true;
        this.fileList.pop();
        this.fileList.push({name: file.name, url: this.dataObj.host + '/' + this.dataObj.key.replace("${filename}",file.name) });
        this.emitInput(this.fileList[0].url);
      },
      init (id) {
            this.getCatesList()
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/mybog/post/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.text = data.post.text
          /*       this.dataForm.createTime = data.post.createTime */
                this.dataForm.isTop = data.post.isTop
                this.dataForm.imge = data.post.imge
                this.dataForm.title = data.post.title
                this.dataForm.summary = data.post.summary
                this.dataForm.categoryId = data.post.categoryId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
            this.$http({
              url: this.$http.adornUrl(`/mybog/post/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'text': this.dataForm.text,
              /*   'createTime': this.dataForm.createTime, */
                'isTop': this.dataForm.isTop,
                'imge': this.dataForm.imge,
                'title': this.dataForm.title,
                'summary': this.dataForm.summary,
                'categoryId': this.dataForm.categoryId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
         
     
      }
    }
  }
</script>
